package com.solidumgame.martillo.hammer
{
	import com.solidumgame.martillo.level.event.LevelGenericEvent;
	
	import com.pblabs.engine.PBE;
	import com.pblabs.engine.entity.EntityComponent;
	import com.solidumgame.martillo.hammer.events.HammerSpeedEvent;
	
	public class HammerSpeedComponent extends EntityComponent
	{
		protected var hammerSpeed:Number;
		
		public function HammerSpeedComponent()
		{
		}
		
		/**
		 * Se agrega listener de actualización de level. 
		 * 
		 */		
		override protected function onAdd():void{
			super.onAdd();
			PBE.mainStage.addEventListener(LevelGenericEvent.UPDATE_EVENT,updateSpeed);
		}
		
		/**
		 * Se remueve listener agregado en onAdd.
		 * 
		 */		
		override protected function onRemove():void{
			super.onRemove();
			PBE.mainStage.removeEventListener(LevelGenericEvent.UPDATE_EVENT,updateSpeed);
		}
		
		/**
		 * Se establece el parámetro hammerSpeed de acuerdo al level del evento escuchado.
		 * @param e Evento de Level.
		 * 
		 */		
		protected function updateSpeed(e:LevelGenericEvent):void{
			
			if(e.level<=10){
			this.hammerSpeed = 3;
			}
			else if(e.level<=20){
				this.hammerSpeed = 2.25;
			}
			else if(e.level<=30){
				this.hammerSpeed = 1.5;
			}
			else if(e.level<=40){
				this.hammerSpeed = 1;
			}
			else if(e.level>40){
				this.hammerSpeed = 0.8;
			}
			PBE.mainStage.dispatchEvent(new HammerSpeedEvent(HammerSpeedEvent.SPEED_UPDATE_EVENT,hammerSpeed));
				
		}
		
	}
}